package com.pai4j.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2024/1/17 22:25
 * @description
 */
public class AiResponseUtil {


    private static final String JSON_START = "```json";
    private static final String JSON_END = "```";


    public static String getJsonString(String content) {
        if (StringUtils.isBlank(content)) {
            return content;
        }

        // 如果没有markdown代码块，直接返回原内容
        if (content.indexOf(JSON_START) == -1) {
            return content;
        }

        // 提取JSON内容
        int jsonStartIndex = content.indexOf(JSON_START);
        content = content.substring(jsonStartIndex + JSON_START.length());

        int jsonEndIndex = content.indexOf(JSON_END);
        if (jsonEndIndex != -1) {
            content = content.substring(0, jsonEndIndex);
        }

        // 只移除开头和结尾的空白字符，保留JSON内部的空格
        return content.trim();
    }
}
